//
//  OutstandingListController.swift
//  ExpressDriver
//
//  Created by sheng on 2017/11/15.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import SnapKit
import MJRefresh

class OutstandingListController: UIViewController, UITableViewDataSource, UITableViewDelegate, OrderCellDelegate {
    
    @IBOutlet weak var orderTabview: UITableView!
    
    let freshHeader = MJRefreshNormalHeader()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    
    func setUI() {
        orderTabview.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalTo(self.view)
        }
        
        freshHeader.setTitle("Pull to refresh", for: .idle)
        freshHeader.setTitle("Release to refresh", for: .pulling)
        freshHeader.setTitle("Loading...", for: .refreshing)
        //header.lastUpdatedTimeLabel.isHidden = true
        freshHeader.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        orderTabview!.mj_header = freshHeader
        
        
        self.orderTabview.dataSource = self
        self.orderTabview.delegate = self
        let nib = UINib(nibName: String(describing: OrderCellTableViewCell.self), bundle: nil)
        self.orderTabview.register(nib, forCellReuseIdentifier: "orderItem")
        self.orderTabview?.estimatedRowHeight = 44.0
        self.orderTabview?.rowHeight = UITableViewAutomaticDimension
        
    }
    
    
    func headerRefresh(){
        print("下拉刷新.")
        sleep(2)
        //重现加载表格数据
        orderTabview!.reloadData()
        //结束刷新
        orderTabview!.mj_header.endRefreshing()
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "orderItem", for: indexPath) as! OrderCellTableViewCell
        cell.selectionStyle = .none
        cell.delegate = self
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("第\(indexPath.row)行被点击了")
    }
    
    
    
    func viewOrderDetail(label: UILabel) {
        
        let orderController = OrderDetailController();
        orderController.title = "Detail"
        self.navigationController?.pushViewController(orderController, animated: true)
    }
    
    
    func printOrder(label: UILabel) {
        let printController = PrintController();
        printController.title = "Print"
        self.navigationController?.pushViewController(printController, animated: true)
    }
    
    func signatureOrder(label: UILabel) {
        let signatureController = SignatureController()
        signatureController.title = "Signature"
        self.navigationController?.pushViewController(signatureController, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    

}
